﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.OA;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.OA
{
    public class DepartmentServices : IDepartmentServices
    {
        public DepartmentServices(IBaseRepository db)
        {
            Db = db;
        }
        public IBaseRepository Db { get; }
        public async Task<bool> AddAsync(Department model)
        {

            var result = await Db.InsertAsync(model);
            return result;
        }

        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete Department where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete Department where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }

        public async Task<IEnumerable<Department>> GetAllAsync()
        {
            string sql = "select * from Department where Status='0' ";
            var list = await Db.QueryAllAsync<Department>(sql);
            return list;
        }

        public async Task<IEnumerable<Department>> GetAsync(int PageIndex, int PageSize, string Number, string Name)
        {
            string sql = "select * from Department where 1=1";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number";
            }
            if (!string.IsNullOrEmpty(Name))
            {
                sql += " and Name like @Name";
            }
          
            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only";

            var list = await Db.QueryAsync<Department>(sql, new { PageIndex, PageSize, Number = $"%{Number}%", Name = $"%{Name}%" });
            return list;
        }

        public async Task<int> GetCountAsync(string Number,string Name)
        {
            string sql = "select * from Department where 1=1";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number";
            }
            if (!string.IsNullOrEmpty(Name))
            {
                sql += " and Name like @Name";
            }

            var list = await Db.GetRecoreCountAsync(sql, new { Number = $"%{Number}%", Name = $"%{Name}%" });
            return list;
        }
        public async Task<bool> UpdateAsync(Department model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }
    }
}
